/**
 * Created by CHENJI5 on 5/27/2016.
 */

$(document).ready(function(){

    /*promoCity List*/
    var cityList = [
        /*{"images":"hongkong","alert":true,"pol":"Southampton","pod":"Melbourne","promoStyle":"1","timeAlert":true,"transhipmentPort":[{
            "port":"Singapore"
        }]},
        */
        {"images":"hongkong","alert":true,"pol":"Southampton","pod":"Melbourne","promoStyle":"1","timeAlert":true},
        {"images":"hongkong","pol":"Southampton","pod":"Sydney","promoStyle":"2","timeAlert":true } ,
        {"images":"default","pol":"Southampton","pod":"Melbourne","promoStyle":"3","alert":true,"saleAlert":"Lower Price" }
    ]
    /*Define show number of promo city when different screen*/
    var screenWidth =$(window).width();
    var displayPromoNum = 1;
    if(screenWidth>=1200){
        displayPromoNum = 3;
    }else if(screenWidth <1200 && screenWidth>=992){
        displayPromoNum = 2;
    }else{
        displayPromoNum = 1;
    }
    if($("body").hasClass("oal-layout-subPage")){
        displayPromoNum = 1;
        if(screenWidth<1200){
            $(".oal-page-content-section").addClass("oal-page-content-sub-sm");
            $(".oal-page-content-item-home").hide()
        }
    }
    /*Define no promo city*/
    if(cityList.length == 0){
        $(".oal-page-content-promo").hide();
        if($("body").hasClass("oal-layout-subPage")==false){
            $(".oal-page-content-no-promo").show();
        }

    }else{
        loadPromoCity(displayPromoNum);
    }

    $('[data-toggle="tooltip"]').tooltip();
    function loadPromoCity(displayPromoNum){

        var cityListLength = cityList.length;
        var cityListOverallDom= $(".oal-promo-item-list-all");
        var cityRow = parseInt((cityListLength-1)/displayPromoNum);
        var cityColumn = cityListLength%displayPromoNum;
        cityListOverallDom.html("");
        for(var m=0;m<cityRow+1;m++){
            var newCityListRowDom = "<div class='row oal-promo-item-list'></div>";
            var newOverlallTemplate = $(".oal-promo-item-detail-overall-template").clone().removeClass("oal-promo-item-detail-overall-template");
            cityListOverallDom.append(newCityListRowDom);
            cityListOverallDom.append(newOverlallTemplate);
        }
        var overallCityRowDom = $(".oal-promo-item-list-all .oal-promo-item-list");
        var cityTemplateDom = $(".oal-promo-item-template .oal-promo-item");
        if(cityColumn == 1){
            for(var i=0;i<cityListLength;i++){
                var newCityItem = cityTemplateDom.clone();
                var cityRowNumber;
                if(i==0){
                    var cityRowNumber = 0;
                    if($("body").hasClass("oal-layout-subPage")== false){
                        newCityItem.addClass("col-lg-12 col-md-12 col-sm-12 oal-promo-item-big");
                    }else{
                        newCityItem.addClass("col-lg-12 col-md-12 col-sm-12");
                    }

                }
                if(i>0){
                    var cityRowNumber = parseInt((i-1)/displayPromoNum)+1;
                    if(displayPromoNum==1){
                        newCityItem.addClass("col-lg-12 col-md-12 col-sm-12");
                    }else if(displayPromoNum == 2){
                        newCityItem.addClass("col-lg-6 col-md-6 col-sm-6");
                    }else if(displayPromoNum == 3){
                        newCityItem.addClass("col-lg-4 col-md-4 col-sm-12");
                    }
                }
                loadPromoCityDetail(newCityItem,i,cityRowNumber)
            }
        }
        if(cityListLength%displayPromoNum == 2){
            for(var i=0;i<cityListLength;i++){
                var newCityItem = cityTemplateDom.clone();
                var cityRowNumber = parseInt((i-2)/displayPromoNum);
                if(i<=1){
                    newCityItem.addClass("col-lg-6 col-md-6 col-sm-12");
                }
                if(i>1){
                    if(displayPromoNum==1){
                        newCityItem.addClass("col-lg-12 col-md-12 col-sm-12");
                    }else if(displayPromoNum == 2){
                        newCityItem.addClass("col-lg-6 col-md-6 col-sm-6");
                    }else if(displayPromoNum == 3){
                        newCityItem.addClass("col-lg-4 col-md-4 col-sm-12");
                    }
                    cityRowNumber = cityRowNumber+1;
                }
                loadPromoCityDetail(newCityItem,i,cityRowNumber)

            }
        }
        if(cityListLength%displayPromoNum == 0){
            for(var i=0;i<cityListLength;i++) {
                var newCityItem = cityTemplateDom.clone();
                var cityRowNumber = parseInt(i / displayPromoNum);
                if(displayPromoNum==1){
                    newCityItem.addClass("col-lg-12 col-md-12 col-sm-12");
                }else if(displayPromoNum == 2){
                    newCityItem.addClass("col-lg-6 col-md-6 col-sm-6");
                }else if(displayPromoNum == 3){
                    newCityItem.addClass("col-lg-4 col-md-4 col-sm-12");
                }
                if(displayPromoNum == 1){
                    newCityItem.addClass("oal-promo-item-sm");

                }
                loadPromoCityDetail(newCityItem,i,cityRowNumber)
            }
        }

        function loadPromoCityDetail(newCityItem,i,cityRowNumber){
            newCityItem.find(".oal-promo-item-bg-image").css("background-image","url(../../common/images/city/"+cityList[i].images+".jpg)")
            newCityItem.find(".detail-por").text(cityList[i].pol);
            newCityItem.find(".detail-pod").text(cityList[i].pod.toUpperCase());
            /*Chenji5 Jul 8*/
            var pod = cityList[i].pod;
            var podNumber = pod.split("/");
            newCityItem.find(".oal-promo-item-sale-text-port").text(pod).css("height","18px");
            var podHtml = "";
            for(var m=0;m<podNumber.length;m++){
                if(m < podNumber.length-1){
                    podHtml +="<span>"+podNumber[m]+"/</span>";
                }else{
                    podHtml +="<span>"+podNumber[m]+"</span>";
                }
            }
            newCityItem.find(".detail-pod").html(""+podHtml);
            newCityItem.find(".oal-promo-item-sale-text-sale-more").hide();
            /*End*/
            if(cityList[i].alert == true){
                newCityItem.find(".oal-promo-item-alert").removeClass("hide").addClass("alert-animation");
            }
            if(cityList[i].promoStyle){
                newCityItem.find(".style-"+cityList[i].promoStyle).removeClass("hide")
            }
            if(cityList[i].timeAlert == true){
                newCityItem.find(".oal-promo-item-time-alert").removeClass("hide");
                newCityItem.find(".oal-promo-item-sale").addClass("oal-promo-item-sale-alert");
                newCityItem.find(".oal-promo-item-sale-text-sale-more").show();
            }
            if(cityList[i].hasRecords == true){
                newCityItem.find(".oal-promo-item-actions").addClass("hasRecords").find(".oal-promo-item-actions-records").removeClass("hide");
            }
            if(cityList[i].hasSold == true){
                newCityItem.find(".oal-promo-item-sold").removeClass("hide");
                newCityItem.find(".oal-promo-item-actions-book").addClass("disable")
            }
            /*
            if( cityList[i].isTranshipment== true){
                newCityItem.find(".oal-promo-item-detail-ss-transhipment-arrow").addClass("isTrans")
            }
            */
            if(cityList[i].promoStyle == "3"){

             var saleAlertLength = cityList[i].saleAlert.length;
               if(saleAlertLength <8){
                   newCityItem.find(".oal-promo-item-sale-text-sale-per-title").addClass("bigSize");
              }else if(saleAlertLength >=8 && saleAlertLength <16){
                  newCityItem.find(".oal-promo-item-sale-text-sale-per-title").addClass("midSize");
              }
              newCityItem.find(".oal-promo-item-sale-text-sale-per-title").text(cityList[i].saleAlert)
          }

            newCityItem.appendTo(overallCityRowDom.eq(cityRowNumber));
            newCityItem.attr("data-index",i);

        }
        $(".oal-promo-item-bg").bind("click",function(){
            $(this).parent().find(".oal-promo-item-actions-detail").click();
        })
        $(".oal-promo-item-actions-detail").bind("click",function(){

            if(promoItemDetailActive){
                $(promoItemDetailActive).removeClass("oal-promo-item-actions-detail-active")
                $(".oal-promo-item-detail-overall").removeClass("oal-promo-item-detail-overall-display")
                $(promoItemDetailActive).parent().parent().parent().find(".oal-promo-item-detail-info").removeClass("opacity");
                $(promoItemDetailActive).parent().parent().parent().find(".oal-promo-item-sale").removeClass("detail-active");
                $(promoItemDetailActive).parent().parent().parent().find(".oal-promo-item-actions-detail").text("Click for Details").append("&nbsp;<span class='glyphicon glyphicon-menu-down'>");
                $(promoItemDetailActive).parent().parent().parent().parent().next().addClass("hide");
                /*Chenji5 Jul 8*/
                $(promoItemDetailActive).parent().parent().parent().find(".oal-promo-item-sale-text-port").css("height","18px");
                /*end*/
                if(promoItemDetailActive == this){
                    promoItemDetailActive = undefined;
                    loadElementSize();
                    return;
                }
            }
            $(this).parent().parent().parent().parent().next().removeClass("hide");
            $(this).parent().parent().parent().find(".oal-promo-item-sale-text-port").css("height","auto")
            if(displayPromoNum == 1){
                $(".oal-promo-item-detail-overall").addClass("oal-promo-item-detail-overall-sm");
            }else{
                $(".oal-promo-item-detail-overall").removeClass("oal-promo-item-detail-overall-sm");
            }
            $(this).addClass("oal-promo-item-actions-detail-active");
            $(".oal-promo-item-detail-overall").addClass("oal-promo-item-detail-overall-display");
            $(this).parent().parent().parent().find(".oal-promo-item-detail-info").addClass("opacity");
            $(this).parent().parent().parent().find(".oal-promo-item-sale").addClass("detail-active");
            var dataIndex = $(this).parent().parent().parent().attr("data-index");
            /*Chenji5 Jul 8*/
            var podLength = cityList[dataIndex].pod.split("/");
            if(podLength.length>1){
                $(this).parent().parent().parent().find(".oal-promo-item-sale-text").addClass("mulPol");

            }

            /*end*/

            $(this).parent().parent().parent().find(".oal-promo-item-actions-detail").text("Promotion Details").append("&nbsp;<span class='glyphicon glyphicon-menu-up'>");

            var detailDom = $(this).parent().parent().parent().parent().next();
            detailDom.find(".detail-por .city").text(cityList[dataIndex].pol);
            detailDom.find(".detail-pod .city").text(cityList[dataIndex].pod);
            detailDom.find(".tsPort").remove();
            detailDom.find(".oal-promo-item-detail-ss-transhipment-arrow-clone").remove();
            if(cityList[dataIndex].transhipmentPort&&cityList[dataIndex].transhipmentPort.length!=0){
                var transhipmentPort = cityList[dataIndex].transhipmentPort;
                var vesselIcon = detailDom.find(".oal-promo-item-detail-ss-transhipment-arrow").clone().addClass("oal-promo-item-detail-ss-transhipment-arrow-clone");
                 for(var m=0;m<transhipmentPort.length;m++){
                    var tSPortEle = "<span class='tsPort'>"+transhipmentPort[m].port+"</span>";

                    $(tSPortEle).insertBefore(detailDom.find(".detail-pod"))
                    vesselIcon.insertBefore(detailDom.find(".detail-pod"))
                }
            }
            promoItemDetailActive = this;
            loadElementSize();
        })
        $(".btn-promo-action").bind("click",function(){
            $("#btn_chat").click();
        })


    }



    var promoItemDetailActive;
    $("body").bind("click",function(){
        $(".hamburger").addClass("active-end");
        $(".oal-page-header-actions").removeClass("active");
    })
    $(".hamburger").bind("click",function(e){
        e.stopPropagation();
        e.preventDefault();
        if($(".hamburger").hasClass("active")==false){
            $(".oal-page-header-actions").addClass("active");
            $(".hamburger").addClass("active");
        }else{
            if($(".hamburger").hasClass("active-end")){
                $(".hamburger").removeClass("active-end");
                $(".oal-page-header-actions").addClass("active");
            }else{
                $(".hamburger").addClass("active-end");
                $(".oal-page-header-actions").removeClass("active");
            }
        }
    })
    var newChangePromoNum = displayPromoNum;
    $("#btn_chat").bind("click",function(){
        $("body").toggleClass("oal-page-chat-section-expanded");
        $(".oal-page-chat-layer").toggleClass("oal-page-chat-layer-expanded");
        $(".oal-page-chat-section .arrow").toggleClass("arrow-expanded");
        $(".oal-page-chat-active-icon").toggleClass("oal-page-chat-active-icon-show");

        var timeOutNum = 1;
        var changeWidth = setTimeout(onWidthChange,10);

        var expandNumber;

        function onWidthChange(){
            var contentWidth = $(".oal-page-content").width();
            if($(".oal-page-chat-layer").hasClass("oal-page-chat-layer-expanded")){

                if(contentWidth>=1200){
                    expandNumber = 3;
                }else if(contentWidth <1200 && contentWidth>=916){
                    expandNumber = 2;
                    $(".oal-page-content-section").addClass("oal-page-content-sub-sm");
                    if($("body").hasClass("oal-layout-subPage")){
                        $(".oal-page-content-item-home").hide(500,function(){
                            loadElementSize();
                        })
                    }
                }else{

                    $(".oal-page-content-section").addClass("oal-page-content-sub-sm");
                    $(".oal-page-header").addClass("oal-page-header-sm");
                    $(".oal-page-footer").addClass("oal-page-footer-sm");
                    if($("body").hasClass("oal-layout-subPage")){
                        $(".oal-page-content-item-home").hide(500,function(){
                            loadElementSize();
                        })
                    }
                    expandNumber = 1;
                }
                if( newChangePromoNum != expandNumber) {
                    loadPromoCity(expandNumber);
                    newChangePromoNum = expandNumber;
                }

            }else{
                $(".oal-page-content-section").removeClass("oal-page-content-sub-sm");
                $(".oal-page-header").removeClass("oal-page-header-sm");
                $(".hamburger").removeClass("active-end").removeClass("active");
                $(".oal-page-header-actions").removeClass("active");
                $(".oal-page-footer").removeClass("oal-page-footer-sm");
                $(".oal-page-content-item-home").show(500,function(){
                    loadElementSize();
                })
                if( newChangePromoNum != displayPromoNum) {
                    loadPromoCity(displayPromoNum);
                }
                newChangePromoNum = displayPromoNum;

                timeOutNum = 49;
                clearTimeout(changeWidth);
            }
            timeOutNum = timeOutNum +1;
            if(timeOutNum == 50){
                clearTimeout(changeWidth);
                return;
            }
            setTimeout(onWidthChange,10);
         }

    })
    $(".oal-page-booking-item").bind("click",function(){
        window.open('booking.html');
    })

    $(".oal-page-side-top").bind("click",function(){
        $('html, body').animate({
            'scrollTop': 0
        }, 300);
    })
    $(".oal-page-header-menu-promotion").bind("click", function () {
        window.location.href="homepage.html"
    })
    $(".oal-page-header-menu-myRate").bind("click", function () {
        window.location.href="homepage_myRates.html"
    })
    $(".oal-page-header-menu-myShipment").bind("click", function () {
        window.location.href="homepage_myShipment.html"
    })
    $(".oal-page-header-menu-notification").bind("click", function () {
        window.location.href="homepage_notification.html"
    })

    $(".oal-page-header-menu-setting").bind("click",function(){
        window.location.href="homepage_setting.html"
    })
    loadElementSize();


    $(window).resize(function() {
        //setPromotionItems(displayPromoNum);
        loadElementSize(displayPromoNum);
        var screenChangeWidth = $(".oal-page-content").width();
        var newDisplayPromoNum;
        if(screenChangeWidth>=1200){
            if(screenChangeWidth>=1440){
                $(".oal-promo-item-detail-overall-detail-result-section1").removeClass("col-lg-12");
                $(".oal-promo-item-detail-overall-detail-result-section2").removeClass("col-lg-5");
                $(".oal-promo-item-detail-overall-detail-result-section3").removeClass("col-lg-7");

                $(".special-requirement-margin").removeClass("col-lg-12");
                $(".special-requirement-margin").addClass("hidden-lg");
                $(".oal-promo-item-detail-overall-detail-title").removeClass("text-center");



                $(".oal-promo-item-detail-overall-detail-result-section1").addClass("col-lg-5");
                $(".oal-promo-item-detail-overall-detail-result-section2").addClass("col-lg-3");
                $(".oal-promo-item-detail-overall-detail-result-section3").addClass("col-lg-4");
            }
            else{
                $(".oal-promo-item-detail-overall-detail-result-section1").removeClass("col-lg-5");
                $(".oal-promo-item-detail-overall-detail-result-section2").removeClass("col-lg-3");
                $(".oal-promo-item-detail-overall-detail-result-section3").removeClass("col-lg-4");

                $(".special-requirement-margin").removeClass("hidden-lg");
                $(".special-requirement-margin").addClass("col-lg-12");

                $(".oal-promo-item-detail-overall-detail-title").addClass("text-center");


                $(".oal-promo-item-detail-overall-detail-result-section1").addClass("col-lg-12");
                $(".oal-promo-item-detail-overall-detail-result-section2").addClass("col-lg-5");
                $(".oal-promo-item-detail-overall-detail-result-section3").addClass("col-lg-7");
            }

            newDisplayPromoNum = 3;
            $(".oal-page-content-section").removeClass("oal-page-content-sub-sm");
            $(".oal-page-header").removeClass("oal-page-header-sm");
            $(".hamburger").removeClass("active-end").removeClass("active");
            $(".oal-page-header-actions").removeClass("active");
            $(".oal-page-footer").removeClass("oal-page-footer-sm");
            $(".oal-page-content-item-home").show(500,function(){
                loadElementSize();
            })
        }else if(screenChangeWidth <1200 && screenChangeWidth>=916){
            newDisplayPromoNum = 2;
            $(".oal-page-content-section").addClass("oal-page-content-sub-sm");
            $(".oal-page-header").removeClass("oal-page-header-sm")
            if($("body").hasClass("oal-layout-subPage")){
                $(".oal-page-content-item-home").hide(500,function(){
                    loadElementSize();
                })
            }
        }else{
            newDisplayPromoNum = 1;
            $(".oal-page-content-section").addClass("oal-page-content-sub-sm");
            $(".oal-page-header").addClass("oal-page-header-sm")
            if($("body").hasClass("oal-layout-subPage")){
                $(".oal-page-content-item-home").hide(500,function(){
                    loadElementSize();
                })
            }
        }
        if(displayPromoNum !=newDisplayPromoNum){
            displayPromoNum = newDisplayPromoNum;
            loadPromoCity(displayPromoNum);

        }
    });

    var backIconPosition = $(".news-content-left-bar-elements").position();

    $(window).scroll(function() {
       $(".oal-page-header-fixed").css("top",$(window).scrollTop()+"px");
        if($(".oal-page-content-news-item-detail").is(":visible")){
            var scrollTop = $(window).scrollTop();
            var backIconPosition = $(".news-content-left-bar-elements").position();
        }

    });

    $(".oal-page-content-news-list .news-text-title,.oal-page-content-news-list .news-text-details").bind("click",function(){
        $(".oal-page-content-news-list").hide();
        $(".oal-page-content-news-item-detail").show();
        loadElementSize();
    })

    $(".news-content-left-bar").bind("click",function(){
        $(".oal-page-content-news-list").show();
        $(".oal-page-content-news-item-detail").hide();
        loadElementSize();
    })
    $(".oal-page-header-logo").bind("click",function(){
        window.location.href="homepage.html"
    })

    iniPromotionDetailWidth();
 })

function loadElementSize(displayPromoNum){

    /*footer*/
    var windowHeight = $(window).height();

    var contentWidth = $(".oal-page-content").width();

    /*iFrame*/
    $("#chatIframe").css("height",windowHeight-40+"px");
    var iframeHeight;
    if($(".oal-page-content-promo").height()+$(".oal-page-content-news-section").height()<$(window).height()){
        iframeHeight = $(window).height();
        if($(".oal-page-footer-sm").length!=0){
            $("#rightIframe").css("height",iframeHeight-240+"px");
        }else{
            $("#rightIframe").css("height",iframeHeight-100+"px");
        }
     }else{
        iframeHeight = $(".oal-page-content-promo").height()+$(".oal-page-content-news-section").height()
         if($(".oal-page-footer-sm").length!=0){
            $("#rightIframe").css("height",iframeHeight-30+"px");
        }else{
            $("#rightIframe").css("height",iframeHeight-70+"px");
        }
    }
    var contentHeight = $(".oal-page-content").height();
    var footerHeight = $(".oal-page-footer").height();
    if(contentHeight + footerHeight > windowHeight){
        $(".oal-page-footer").css("position","static")
    }else{
        if($(".oal-page-content-sub-sm").length !=0){
            $(".oal-page-footer").css("position","absolute").css("top",windowHeight-90+"px");
        }else{
            $(".oal-page-footer").css("position","absolute").css("top",windowHeight-130+"px");
        }

    }
}

function iniPromotionDetailWidth(){
    var screenChangeWidth = $(".oal-page-content").width();
    if(screenChangeWidth>=1200){
        if(screenChangeWidth>=1440){
            $(".oal-promo-item-detail-overall-detail-result-section1").removeClass("col-lg-12");
            $(".oal-promo-item-detail-overall-detail-result-section2").removeClass("col-lg-5");
            $(".oal-promo-item-detail-overall-detail-result-section3").removeClass("col-lg-7");

            $(".special-requirement-margin").removeClass("col-lg-12");
            $(".special-requirement-margin").addClass("hidden-lg");
            $(".oal-promo-item-detail-overall-detail-title").removeClass("text-center");



            $(".oal-promo-item-detail-overall-detail-result-section1").addClass("col-lg-5");
            $(".oal-promo-item-detail-overall-detail-result-section2").addClass("col-lg-3");
            $(".oal-promo-item-detail-overall-detail-result-section3").addClass("col-lg-4");
        }
        else{
            $(".oal-promo-item-detail-overall-detail-result-section1").removeClass("col-lg-5");
            $(".oal-promo-item-detail-overall-detail-result-section2").removeClass("col-lg-3");
            $(".oal-promo-item-detail-overall-detail-result-section3").removeClass("col-lg-4");

            $(".special-requirement-margin").removeClass("hidden-lg");
            $(".special-requirement-margin").addClass("col-lg-12");

            $(".oal-promo-item-detail-overall-detail-title").addClass("text-center");


            $(".oal-promo-item-detail-overall-detail-result-section1").addClass("col-lg-12");
            $(".oal-promo-item-detail-overall-detail-result-section2").addClass("col-lg-5");
            $(".oal-promo-item-detail-overall-detail-result-section3").addClass("col-lg-7");
        }
    }

}

